package cst.a113cx.fragmentrecyclerview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.AndroidCharacter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import Bean.LoginBean;
import Retorfit对象.LoginModel;
import retrofit2.Call;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {

    private Button b01;
    private EditText et01,et02;
    private TextView tv01,tv02,tv03,tv04,tv05,tv06;
    private SharedPreferences sp;

    void spput(LoginBean a){
        sp=getSharedPreferences("login",MODE_PRIVATE);
        SharedPreferences.Editor  editor=sp.edit();
        editor.putString("name",a.getUsername());
        editor.putString("sessionid",a.getSessionid());
        editor.commit();
    }

    void init(){
        tv01=(TextView)findViewById(R.id.textView11);
        tv02=(TextView)findViewById(R.id.textView12);
        tv03=(TextView)findViewById(R.id.textView13);
        tv04=(TextView)findViewById(R.id.textView14);
        tv05=(TextView)findViewById(R.id.textView15);
        tv06=(TextView)findViewById(R.id.textView16);

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        b01=(Button)findViewById(R.id.btn_login);
        et01=(EditText)findViewById(R.id.et_username) ;
        et02=(EditText)findViewById(R.id.et_password) ;
        //init();
        final Intent intent = new Intent(this,MainActivity.class);
        final Intent intent2=new Intent(this,ShowActivity.class);
        b01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginModel LM= new LoginModel() {
                    @Override
                    public void LoginonResponse(Call<LoginBean> call, Response<LoginBean> response) {
                        if (response.isSuccessful() && response.body()!=null) {

                            LoginBean lb=response.body();
                            if(lb.getId().isEmpty()==false){
                                Toast.makeText(LoginActivity.this,lb.getUsername(), Toast.LENGTH_SHORT).show();
                                //spput(lb);
                                startActivity(intent2);
                              // init();
                                //tv02.setText(lb.getUsername());
//                                tv03.setText(lb.getRealname().toString());
//                                tv04.setText(lb.getRolename().toString());
//                                tv05.setText(lb.getSex().toString());
//                                tv06.setText(lb.getSessionid().toString());
                                }



                        }
                        else
                            Toast.makeText(LoginActivity.this, "没收到", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void LoginonFailure(Call<LoginBean> call, Throwable t) {
                        Toast.makeText(LoginActivity.this, "失败", Toast.LENGTH_SHORT).show();
                    }
                };
                LM.MakeAAsk(et01.getText().toString(),et02.getText().toString());

                if(et01.getText().toString().equals("test")&&et02.getText().toString().equals("test"))
                {//Intent(MainActivity);

                    startActivity(intent);
                    Toast.makeText(LoginActivity.this, "666", Toast.LENGTH_SHORT).show();}


            }
        });
    }
}
